# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv, fields

def get_default(val):
    def fct(self, uid, data, state):
        return val
    return fct

class qty_test_raw(osv.osv_memory):
    """ OpenERP Wizard  Version 7"""

    _name = 'qty.test.raw'

    _columns = {
        'test_date': fields.date('Date'),
        'tester': fields.many2one('hr.employee', 'Tester', required="True"),
        'product': fields.many2one('product.product', 'Product',
                                   readonly='True'),
        'raw_test': fields.related('product','raw_m_test', 
                                  type='one2many', 
                                  relation='quality.test.config', 
                                  string = 'Quality Raw Test',
                                  readonly="True"), 
        'state': fields.selection([('init', 'init'), ('ok', 'ok')])
     }
    
    def check(self, cr, uid, data, context):
        move_id = context['move_id']
        obj = self.browse(cr, uid, data[0])        
        test_obj = self.pool.get('testing.result')
        test_config = self.pool.get('quality.test.config')
        move = self.pool.get('stock.move')
   
        if obj.raw_test:
            flag = False
            res = {
                'type':'rw_mat', 
                'product': obj.product.id, 
                'tester': obj.tester.id, 
                'test_date': obj.test_date
            }
            test_id = test_obj.create(cr, uid, res)
            selected_ids = context.get("raw_test_selected", []) 
            actual = context.get("actual_value", 0)   
            for case in obj.raw_test:    
#                 actual = case.actual_val
                min_val = case.min_limit
                max_val = case.max_limit
    
                if case.id in selected_ids:
                    if actual > 0.00:
                        val = {}
                        if (actual >= min_val and actual <= max_val):
                            state = 'accepted'
                        else:
                            state = 'rejected'
                            flag = True
                        val = {
                           'name':case.name.id, 'min_limit':case.min_limit, 
                           'max_limit':case.max_limit, 'uom':case.uom.id,
                           'actual_val':actual, 'state':state, 
                           'test_id':test_id
                        }
                        test_config.create(cr, uid, val)
    
            if flag:
                move.write(cr, uid, move_id, {'qlty_test_reject':True})
                move.write(cr, uid, move_id, {'qlty_test_accept':False})
                move.write(cr, uid, move_id, {'state':'confirmed'})
            else:
                move.write(cr, uid, move_id, {'qlty_test_accept':True})
                move.write(cr, uid, move_id, {'qlty_test_reject':False})
    
        return {'type': 'ir.actions.act_window_close'}    

qty_test_raw()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
